package cn.edu.zjicm.wordsnet_d.view;

import cn.edu.zjicm.wordsnet_d.R;
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.view.View.MeasureSpec;
import android.view.View.OnClickListener;

public class MyTabPageIndicator extends HorizontalScrollView implements OnClickListener
{
	private Context context;
	private LinearLayout mTabLayout;
	private int mMaxTabWidth;
	private int mSelectedTabIndex;
	private Runnable mTabSelector;
	private Handler handler;
	private int index;

	public MyTabPageIndicator(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		// TODO Auto-generated constructor stub
		this.context = context;
		mTabLayout = new LinearLayout(context);
		setLayoutParams(new android.widget.AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		addView(mTabLayout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		setHorizontalScrollBarEnabled(false);
		setBackgroundColor(Color.WHITE);

	}

	public void setHandler(Handler handler)
	{
		this.handler = handler;
	}

	@Override
	public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
	{
		final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
		final boolean lockedExpanded = widthMode == MeasureSpec.EXACTLY;
		setFillViewport(lockedExpanded);

		final int childCount = mTabLayout.getChildCount();
		if (childCount > 1 && (widthMode == MeasureSpec.EXACTLY || widthMode == MeasureSpec.AT_MOST))
		{
			if (childCount > 2)
			{
				mMaxTabWidth = (int) (MeasureSpec
						.getSize(widthMeasureSpec) * 1.0f/3);
			}
			else
			{
				mMaxTabWidth = MeasureSpec
						.getSize(widthMeasureSpec) / 2;
			}
		}
		else
		{
			mMaxTabWidth = -1;
		}

		final int oldWidth = getMeasuredWidth();
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		final int newWidth = getMeasuredWidth();

//		if (lockedExpanded && oldWidth != newWidth)
//		{
//			// Recenter the tab display if we're at a new
//			// (scrollable) size.
//			setCurrentItem(mSelectedTabIndex);
//		}
	}

	private void animateToTab(final int position)
	{
		final View tabView = mTabLayout.getChildAt(position);
		if (mTabSelector != null)
		{
			removeCallbacks(mTabSelector);
		}
		mTabSelector = new Runnable()
		{
			public void run()
			{
				final int scrollPos = tabView.getLeft() - (getWidth() - tabView
						.getWidth()) / 2;
				smoothScrollTo(scrollPos, 0);
				mTabSelector = null;
			}
		};
		post(mTabSelector);
	}

	public void setCurrentItem(int item)
	{
		mSelectedTabIndex = item;
		index = item;
		final int tabCount = mTabLayout.getChildCount();
		for (int i = 0; i < tabCount; i++)
		{
			final TabView child = (TabView) mTabLayout
					.getChildAt(i);
			final boolean isSelected = (i == item);
			child.setUnselect();
			if (isSelected)
			{
				child.setSelect();
				animateToTab(item);
			}
		}
	}

	public int getIndex()
	{
		return index;
	}

	private class TabView extends LinearLayout
	{
		private int mIndex;
		private LinearLayout item;
		public TextView itemText;

		@Override
		public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
		{
			

			// Re-measure if we went beyond our maximum size.
			if (mMaxTabWidth > 0 )
			{
				super.onMeasure(MeasureSpec
						.makeMeasureSpec(mMaxTabWidth, MeasureSpec.EXACTLY), heightMeasureSpec);
			}
			else
			{
				super.onMeasure(widthMeasureSpec, heightMeasureSpec);
			}
		}

		public TabView(Context context)
		{
			super(context);
			item = (LinearLayout) LayoutInflater
					.from(context)
					.inflate(R.layout.view_indicator_item, this, false);
			itemText = (TextView) item.findViewById(R.id.indicator_item_tv);
			addView(item);
		}

		public void setUnselect()
		{
			itemText.setTextColor(Color.parseColor("#959595"));
			itemText.setBackgroundColor(Color.TRANSPARENT);
		}

		public void setSelect()
		{
			itemText.setTextColor(Color.parseColor("#ffffff"));
			TypedValue typedValue = new TypedValue();
			context.getTheme().resolveAttribute(R.attr.indicator_corner_bg, typedValue, true);
			itemText.setBackgroundResource(typedValue.resourceId);
		}

		public int getIndex()
		{
			return mIndex;
		}
	}

	public void addTab(int index, CharSequence text)
	{
		final TabView tabView = new TabView(getContext());
		tabView.mIndex = index;
		tabView.setFocusable(true);
		tabView.itemText.setText(text);
		tabView.setId(index);
		tabView.setOnClickListener(this);
		mTabLayout.addView(tabView);
	}

	@Override
	public void onClick(View arg0)
	{
		// TODO Auto-generated method stub
		setCurrentItem(arg0.getId());
		Message msg = new Message();
		msg.what = 1;
		msg.obj = arg0.getId();
		handler.sendMessage(msg);
	}

}
